package com.OnTimeTeam.OnTime;

import java.util.List;
import java.util.Locale;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;

public class FindAddressAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog progressDialog;
    private Dialog dialog;
    private String[] ADDRESSES;
    private String addressForQuery;
    private SmartAlarmFeatures af;
    private String location;
    private Toast resultToast; 
    private TextView toastMessage;
    private String path;
	private SetSmart host;
	
	public FindAddressAsyncTask(SetSmart host, Dialog dialog,final String[] ADDRESSES, String addressForQuery, Toast resultToast, TextView toastMessage, String location, SmartAlarmFeatures af, String path){
		this.host = host;
		this.dialog = dialog;
		this.ADDRESSES = ADDRESSES;
		this.addressForQuery = addressForQuery;
		this.af = af;
		this.location = location;
		this.resultToast = resultToast;
		this.path = path;
		this.toastMessage = toastMessage;
	}
	
    protected Boolean doInBackground(String... params) {//In Background

		int i; //Check if address exists in the addresses list
		for(i = 0; i < ADDRESSES.length; ++i)
			if(ADDRESSES[i].equals(addressForQuery))
				break;
		
		boolean addressIsOk = true;
		
		//if the address given does not exist in list
		if(i == ADDRESSES.length) {
			

			
			Geocoder geocoder = new Geocoder(host, Locale.getDefault());
    		List<Address> geoResults;
			try {
				geoResults = geocoder.getFromLocationName(addressForQuery, 1);
			    if (geoResults.size() == 0)
			    	addressIsOk = false;
			    
			} 
			catch (Exception e) {

				addressIsOk = false;
			}
			
			if(!addressIsOk) {
				
				try {
					
					Thread.sleep(2000);
					addressIsOk = RegularRoadsQuery.addressVerificationByGeoCoder(addressForQuery);
				} 
				catch (Exception e) {
					
					addressIsOk = false;
				}
			}
		}

    	return addressIsOk;
    }      

    protected void onPreExecute() {//Before
    	super.onPreExecute();

    	progressDialog = ProgressDialog.show(host, host.getResources().getString(R.string.validatingAddress), host.getResources().getString(R.string.pleaseWait));
    }
    
    protected void onPostExecute(Boolean result) {//After

    	progressDialog.dismiss();
    	
    	if(result) {
    		////////////
    		//Confirm the address exists
    		resultToast.setDuration(Toast.LENGTH_SHORT);
    		toastMessage.setTextColor(Color.parseColor("#0099CC"));
    		toastMessage.setText(host.getResources().getString(R.string.locationValid));
    		resultToast.show();
    		
        	if(location.equals("origin")) {
        		af.setOrigin(addressForQuery);
        		AddressAutoCompletionIO.addNewAddress(path + "/addresses.xml", af.getOrigin());

        	}
        	else {
        		af.setDest(addressForQuery);
        		AddressAutoCompletionIO.addNewAddress(path + "/addresses.xml", af.getDest());
        	}
        	
        	
    		
        	
        	host.ADDRESSES = AddressAutoCompletionIO.getAllAddresses(path + "/addresses.xml");
        	
        	host.smartListAdapter.notifyDataSetChanged();
        	
        	dialog.dismiss();
    		
    	}
    	else {
    		
    		resultToast.setDuration(Toast.LENGTH_SHORT);
    		toastMessage.setTextColor(Color.parseColor("#CC0000"));
    		toastMessage.setText(host.getResources().getString(R.string.locationInvalid));
    		resultToast.show();
    		
    	}

    }



    protected void onProgressUpdate(Void... values) {
    	
    	
    }
}

